﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using AlexandriaPressUserBusinessLayer;
using System.Data;

namespace AlexandriaPressWebsite.MasterPages.UserPages
{
    public partial class SearchPage : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
      
        }

        protected void searchButton_Click(object sender, EventArgs e)
        {
            UserBusinsessLayer business = new UserBusinsessLayer();

            if (searchItem.Text.Length != 0)
            {
                string searchType = searchTypeDropDownList.SelectedItem.Value;
                switch (searchType)
                {
                    case "Title":
                        DataTable titles = business.searchTitleView(business.searchTitle(searchItem.Text.Trim()));
                        searchGridView1.DataSource = titles;
                        searchGridView1.EmptyDataText = "No matches found for the search criteria";
                        searchGridView1.DataBind();
                        searchAuthorGridView1.Visible = false;
                        searchGridView1.Visible = true;
                        searchResultLabel.Visible = false;
                        searchResultLabel.Visible = false;
                        break;
                    case "Author":
                        DataTable authors = business.searchAuthorView(business.searchAuthor(searchItem.Text.Trim()));
                        searchAuthorGridView1.DataSource = authors;
                        searchAuthorGridView1.EmptyDataText = "No matches found for the search criteria";
                        searchAuthorGridView1.DataBind();
                        searchGridView1.Visible = false;
                        searchAuthorGridView1.Visible = true;
                        searchResultLabel.Visible = false;
                        searchResultLabel.Visible = false;
                        break;
                    case "Genre":
                        DataTable genre = business.searchTitleView(business.searchGenre(searchItem.Text.Trim()));
                        searchGridView1.DataSource = genre;
                        searchGridView1.EmptyDataText = "No matches found for the search criteria";
                        searchGridView1.DataBind();
                        searchAuthorGridView1.Visible = false;
                        searchGridView1.Visible = true;
                        searchResultLabel.Visible = false;
                        searchResultLabel.Visible = false;
                        break;
                }
            }
            else
            {
                searchResultLabel.Text = "You have not entered a search criteria";
                searchAuthorGridView1.Visible = false;
                searchGridView1.Visible = false;
            }
        }

        protected void searchGridView1_OnPageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            UserBusinsessLayer business = new UserBusinsessLayer();
            string searchType = searchTypeDropDownList.SelectedItem.Value;
            if (searchType == "Title")
            {
                searchGridView1.PageIndex = e.NewPageIndex;
                searchGridView1.DataSource = business.searchTitleView(business.searchTitle(searchItem.Text.Trim()));
            }
            else
            {
                searchGridView1.PageIndex = e.NewPageIndex;
                searchGridView1.DataSource = business.searchTitleView(business.searchGenre(searchItem.Text.Trim()));
            }

            searchGridView1.DataBind();
        }

        protected void searchAuthorGridView1_OnPageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            UserBusinsessLayer business = new UserBusinsessLayer();
            searchAuthorGridView1.PageIndex = e.NewPageIndex;
            searchAuthorGridView1.DataSource = business.searchAuthorView(business.searchAuthor(searchItem.Text.Trim()));
            searchAuthorGridView1.DataBind();
        }

        protected void allAuthorsButton_Click(object sender, EventArgs e)
        {
            Response.Redirect("~/MasterPages/UserPages/SearchAllAuthors.aspx");
        }

        protected void mostPopularButton_Click(object sender, EventArgs e)
        {
            Response.Redirect("~/MasterPages/UserPages/MostPopular.aspx");
        }

        protected void allTitlesButton_Click1(object sender, EventArgs e)
        {
            Response.Redirect("~/MasterPages/UserPages/SearchAllTitles.aspx");
        }

    }
}
